package com.vr.xuecheng.media.controller;

import cn.hutool.core.util.StrUtil;
import com.vr.xuecheng.base.exception.XueChengPlusException;
import com.vr.xuecheng.base.response.Result;
import com.vr.xuecheng.media.model.entity.MediaFiles;
import com.vr.xuecheng.media.service.IMediaFilesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hzh
 * @date 2025/03/18
 * 媒资文件打开控制器
 */
@Slf4j
@RestController
@Tag(name = "媒资文件打开控制器")
@RequestMapping("/open")
@RequiredArgsConstructor
public class MediaOpenController {
    private final IMediaFilesService mediaFileService;

    /**
     * @param fileId 文件id
     * @return {@code Result<String> }
     */
    @Operation(summary = "预览播放")
    @GetMapping("/preview/{file-id}")
    public Result<String> getPlayUrlByMediaId(@PathVariable("file-id") String fileId) {
        MediaFiles mediaFiles = mediaFileService.getById(fileId);
        if (mediaFiles == null || StrUtil.isBlank(mediaFiles.getUrl())) {
            XueChengPlusException.cast("视频还没有转码处理");
        }
        return Result.ok(mediaFiles.getUrl());
    }
}
